<?php

/**
 * Description of pages
 *
 * @author iLyas
 */
class Pages extends CI_Controller{
    function __construct() {
        parent::__construct();
        //check if user is logged in
        $this->is_logged_in();
    }
	
    function index(){
        
        $this->load->model('user_model');
        $this->load->model('page_model');
        $this->load->model('dashboard_model');
        
        //Fetch User Page records
        if ($q = $this->page_model->get_user_pages($this->session->userdata('user_id'))) {
            $data['pages_records'] = $q;
        }
        
        $data['userInfo']       = $this->user_model->getUserRecords($this->session->userdata('user_id'));
        $data['language']        = $this->session->userdata('user_lang');
        $this->load->view('user/pages', $data);
    }
    
    function preview($page_id){
        
        $this->load->model('page_model');
        $this->load->model('user_model');
        
        $data['records']= $this->page_model->getPageInfo($page_id);
            
        $page_owner = $this->page_model->pageBelongsTo($page_id);
        
        $data['user_records'] = $this->user_model->getUserRecords($page_owner);
        
        $this->load->view('page', $data);        
    }
    
    function create_new(){
        $data['language'] = $this->session->userdata('user_lang');
        
        if($this->input->post('submit')){
            $this->load->library('form_validation');
            
                //field name, error message, validation rules
                $this->form_validation->set_rules('page_title','Page title','callback_title_check');
                $this->form_validation->set_rules('short_description','Short description','trim|required|max_length[255]');
                $this->form_validation->set_rules('long_description','Long description','trim|required');
                $this->form_validation->set_rules('nbr_cases','Cases number','trim|required|integer');
                $this->form_validation->set_rules('prix_case','Case Price','trim|required|integer');
                $this->form_validation->set_rules('keywords','Keywords','trim|required');
                $this->form_validation->set_rules('deadline','Deadline date','trim|required');
                
                // @TODO: l'ajour du header de la page
                /* @todo: choix de la template de la page */

                
                if($this->form_validation->run() == FALSE ){
                    //$this->create_new();
                    $this->load->view('user/pages_new', $data);
                    //redirect('/user/pages', 'refresh');
                }else{                
                    $this->load->model('page_model');
                    if($q = $this->page_model->create_page($this->session->userdata('user_id'))){
                        // TODO : this->send_email_success();
                        // TODO : take snapshot of the page and save it
                        
                        $data['created_success'] = '1';
                        $this->load->view('user/pages_new', $data);
                    }else{
                        $this->load->view('user/pages_new', $data);
                    }
                }
        }else{
            $this->load->view('user/pages_new', $data);
        }
        
    }
    function title_check(){
        $this->load->model('page_model');
        
        if($q = $this->page_model->page_exists()){
            $this->form_validation->set_message('title_check', 'It seems an other page exists with the same %s');
            return false;
        }else{
            return true;
        }
    }
    
    function edit ($page_id){
         $this->load->view('user/page_edit');
    }
    function delete ($page_id){
         
    }
    function is_logged_in(){
        $is_logged_in = $this->session->userdata('is_logged_in');
        
        if(!(isset($is_logged_in)) || $is_logged_in != TRUE){
             redirect('/user/login/', 'refresh');
        }
    }
    
}

?>
